<?php
include("class/pData.class.php");
include("class/pDraw.class.php");
include("class/pImage.class.php");

date_default_timezone_set('Asia/Shanghai');

$ot = "";
$type = isset($_GET['type']) ? $_GET['type'] : 'line';

$tb_name = "tbl_stock_price_for_chart";

if ($_SERVER["SERVER_NAME"] == '127.0.0.1') {
    $dbh = new PDO('mysql:host=localhost;dbname=test', 'root', '123456');
} else {
    $dbh = new PDO('mysql:host=116.255.150.169;port=4002;dbname=czzzzy', 'czzzzy_f', '123456');
}

$id = isset($_GET['id']) ? (int)$_GET['id'] : 1;
$wheres = "where stock_no = " . $id ." order by created asc limit 20";
$sth = $dbh->prepare("SELECT * FROM $tb_name " . $wheres);
$sth->execute();
$results = $sth->fetchAll(PDO::FETCH_ASSOC);



$line2 = array();
$bar = array();
$times = array();
foreach ($results as $keys => $values) :
    $line2[] = $values['current_price'];
    $bar[] = $values['volume'];
    //$times[] = ($values['datetime'] / 100) . ":" . $values['datetime'] % 100;
    $times[] = $values['created'];
endforeach;

$l2counts = count($line2);

/*
$line2 = array(
    0  => "73170",
    5  => "73170",
    10 => "73300",
    15 => "73170",
    20 => "73220",
    25 => "73350",
    30 => "73150",
    35 => "73150",
    40 => "73150",
    45 => "73150",
    50 => "73150",
    55 => "73180",
    60 => "73100",
   // "73150"
);
*
$l2s = array();
$temp_keys = 0;  //初始key
$temp_arr = array("k" => 0, "v" => 0);
foreach ($line2 as $keys => $values) {
    if ($keys > $temp_keys) {
        for ($i = $temp_keys; $i < $keys; $i++) {
            $l2s[$i + 1] = $values;
        }
        $temp_keys = $keys;
    }
}

//设置昨日收盘价
//line1的点
$l1s = array();
//l1里面的每个点都是同一个值的数组
for ($i = 0; $i < 60; $i++) {
    $l1s[] = 72500;
}
*/

$myData = new pData();

//如果是线型图
if ($type == "line") {

    $sql = "SELECT  MIN(`current_price`) xiao, MAX(`current_price`) da FROM $tb_name $wheres";
    foreach ($dbh->query($sql, PDO::FETCH_ASSOC) as $row) {
        $bottom = (int)$row['xiao'] - 2;
        $top = (int)$row['da'] + 2;
    }

    $l1s = array();
    for ($i = 1; $i <= $l2counts; $i++) {
        $l1s[] = 130;
    }

    $myData->addPoints($l1s, "Line1");
    $myData->addPoints($line2, "Line2");
    $myData->setAxisPosition(0, AXIS_POSITION_RIGHT);
    $myData->addPoints($times, "Times");
    $myData->setSerieDescription("Times","Time");
    $myData->setAbscissa("Times");
    $myData->setXAxisDisplay(AXIS_FORMAT_TIME,"H:i");

    $myPicture = new pImage(480, 300, $myData);
//背景颜色
    //$Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
    //$myPicture->drawGradientArea(0,0,480,300,DIRECTION_VERTICAL,$Settings);

//画格子和标签
    $myPicture->setGraphArea(10, 20, 440, 280);
//设置字体
    $myPicture->setFontProperties(array("FontName" => "fonts/tahoma.ttf", "FontSize" => 6));
    $AxisBoundaries = array(0 => array("Min" => $bottom, "Max" => $top));
    $Settings = array(
        "Mode" => SCALE_MODE_MANUAL,
        "GridR" => 200,
        "GridG" => 200,
        "GridB" => 200,
        "XMargin" => 0,
        "YMargin" => 0,
        //"DrawXLines" => false,
        "GridTicks" => 3,  //格子密度
        "ManualScale" => $AxisBoundaries,
    );
    $myPicture->drawScale($Settings);

//$myPicture->processScale(73450, 72700, 150,  ,0);

//字 808080 128 128 128

//3372b2 51 114 178
//4a82bb 74 130 187
//cc0000 204 0 0
//3372b2 51 114 178

//画线
    $line_arr = array(
        "ForceColor" => TRUE,
        "ForceR" => 51,
        "ForceG" => 114,
        "ForceB" => 178);
    $myPicture->drawLineChart($line_arr);

    $myData->setSerieDrawable("Line1",FALSE);

//画区域底线
    $area_arr = array(
        "DisplayColor" => DISPLAY_AUTO,
    );
    $myPicture->drawAreaChart($area_arr);

//柱形图
} else {

    $myData->addPoints($bar, "Bar");
    $myData->addPoints($times, "Times");
    $myData->setSerieDescription("Times","Time");
    $myData->setAbscissa("Times");
    $myData->setXAxisDisplay(AXIS_FORMAT_TIME,"H:i");

    $myPicture = new pImage(480, 200, $myData);

    $myPicture->Antialias = FALSE;
    $myPicture->setFontProperties(array("FontSize"=>6));

    $myPicture->setGraphArea(50,20,450,180);

    $scaleSettings = array(
        "Mode" => SCALE_MODE_START0,
        "GridR"=>200,
        "GridG"=>200,
        "GridB"=>200);
    $myPicture->drawScale($scaleSettings);

    $Palette = array();
	for ($i = 0; $i <= $l2counts; $i++) {
		$Palette[$i] = array("R"=>74,"G"=>114,"B"=>178,"Alpha"=>100);
	}
	/*
	$Palette = array(
        "0"=>array("R"=>74,"G"=>114,"B"=>178,"Alpha"=>100)); */

    $barSetting = array(
        "OverrideColors"=>$Palette,
		"DisplayR" => 255,
		"DisplayG" => 0,
		"DisplayB" => 0,
		);
    $myPicture->drawBarChart($barSetting);

}

$myPicture->stroke();

$ot .= json_encode($myData->getCoefficientOfVariation("line1"));
file_put_contents("log.txt", json_encode($myData) . "\n" . $ot);
?>